//
//  LKStatusCell.swift
//  LKWeibo
//
//  Created by like on 16/8/13.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit
import SnapKit
class LKStatusCell: UITableViewCell {
    
    var bottomViewTopConstraint: Constraint?
    //定义一个statusViewModel
    var statusViewModel: LKStatusViewModel?{
        didSet{
            //原创微博
            originalView.statusViewModel = statusViewModel
            
            
            //底部微博
            bottomView.statusViewModel = statusViewModel
            /*
             - 转发微博逻辑分析
             - 如果retweeted_status == nil  没有转发微博
             - 底部视图的top == 原创微博的bottom
             
             - 反之 有转发微博
             - 赋值
             - 底部视图的top == 转发微博的bottom
             */

            
            //移除约束
            bottomViewTopConstraint?.uninstall()
           
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status?.text == nil {
                
                bottomView.snp_updateConstraints(closure: { (make) in
                   bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
                
                // 隐藏转发微博
                retweetView.hidden = true
            }else {
                //有转发微博 先赋值
                retweetView.statusViewModel = statusViewModel
                
                bottomView.snp_updateConstraints(closure: { (make) in
                   bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                // 隐藏转发微博
                retweetView.hidden = false
            }
   
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)
        
        //原创
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(8)
            // 该约束已经在自己的类中约束了
            //make.height.equalTo(50)
        }
        // 02转发
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            // 该约束已经在自己的类中约束了
            // make.height.equalTo(50)
        }
        // 03底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
          bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
    }
    
    
    
    
   //懒加载 原创微博
    private lazy var originalView:LKStatusOriginalView = LKStatusOriginalView()
    
    //底部视图
    private lazy var bottomView: LKStatusBottomView = LKStatusBottomView()
    
    // 转发微博
    private lazy var retweetView: LKStatusRetweetView = LKStatusRetweetView()
    

    
}